package com.alstudio.utils.mission.parser.base;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import com.alstudio.utils.mission.ALMissionHttpManager.MissionInfoCallback;
import com.alstudio.utils.mission.MissionInfo;
import com.alstudio.utils.mission.MissionPackageCallback;
import com.alstudio.utils.xml.BaseXmlParser;
import com.alstudio.utils.xml.BaseXmlParser.XmlParseCallback;

public class BaseMissionXmlParser implements MissionPackageCallback {

	protected MissionInfoCallback mListener;
	protected MissionXmlParser mParser;
	protected final String ERROR_TAG = "error";
	protected final String TYPE_TAG = "task_type";
	protected final String TYPE_NAME_TAG = "task_type_name";
	protected final String TYPE_DESC_TAG = "task_type_desc";
	protected final String TYPE_STEP_STATUS_TAG = "step_status";
	protected final String TYPE_ACTIVITY_TAG = "task_activity";
	protected final String ITEM_TAG = "item";
	protected final String STEP_ID_TAG = "task_step_id";
	protected final String STEP_TITLE_TAG = "step_title";
	protected final String STEP_AWARD_DESC_TAG = "step_award_desc";
	protected final String ADD_GOLD_TAG = "addgold";
	protected final String ADD_YINBI_TAG = "addjindou";
	protected final String ADD_GLAMOUR_TAG = "addglamour";
	protected final String ADD_EXP_TAG = "addexp";
	protected final String STEP_STATUS_TAG = "step_status";
	protected final String STEP_ACTIVITY_TAG = "step_activity";
	protected final String TASK_NAME_TAG = "task_name";
	protected final String TASK_DETAIL_TAG = "task_detail";
	protected final String TASK_STATUS_TAG = "task_status";
	protected final String REDIRECT_TAG = "redirect";
	protected final String TASK_AWARD_DESC_TAG = "task_award_desc";
	protected final String TASK_REQUIRE_TAG = "task_require";
	protected final String TASK_DIFFICULTY_TAG = "task_difficulty";
	protected final String ADD_POINT_TAG = "addpoints";

	protected final String INVITE_TYPE_TAG = "invite_type";
	protected final String INVITE_REDIRECT_TAG = "invite_redirect";
	protected final String INVITE_SUM_TAG = "invite_sum";
	protected final String INVITE_TAG = "invite";
	protected final String COMPLETE = "complete";
	protected final String DAILY_COMPLETE = "daily_complete";

	// 猫呼任务
	protected final String DESC_TAG = "desc";
	protected final String STATUS_TAG = "status";
	protected final String TASK_ID_TAG = "taskid";

	protected final String EXP_TAG = "exp";
	protected final String GOLD_TAG = "gold";
	protected final String GIFT_ID_TAG = "giftid";
	protected final String GIFT_NAME_TAG = "giftname";
	protected final String FILEID_TAG = "fileid";
	// 猫呼任务

	// 难度
	protected final String STEP_DIFFICULTY_TAG = "step_difficulty";
	// 任务描述
	protected final String STEP_TASK_DETAIL = "step_task_detail";
	protected MissionInfo info;

	protected ArrayList<MissionInfo> tasks;
	protected int code = 200;
	protected int dailyComplete = 0;

	@Override
	public void parseXmlPackage(String stream, MissionInfoCallback listener)
			throws Exception {
		// TODO Auto-generated method stub
	}

	protected void init(MissionInfoCallback listener) {
		if (tasks == null) {
			tasks = new ArrayList<MissionInfo>();
		} else {
			tasks.clear();
		}
		code = 200;
		mListener = listener;
		info = null;
		dailyComplete = 0;
	}

	protected void startParse(String stream, XmlParseCallback callback) {
		try {
			mParser = new MissionXmlParser(stream, callback);
			mParser.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 读取节点内容
	 * 
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	protected String readText() {
		try {
			return mParser.readText();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取节点所有属性
	 * 
	 * @param tag
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	protected ArrayList<String> getAttributes(final String tag)
			throws XmlPullParserException, IOException {
		return mParser.getAttributes(tag);
	}

	/**
	 * 获取属性内容
	 * 
	 * @param att
	 * @return
	 */
	protected String getAttValue(String att) {
		return mParser.getAttValue(att);
	}

	protected void onCallback() {

	}

	protected class MissionXmlParser extends BaseXmlParser {

		private XmlParseCallback mCallback;

		protected MissionXmlParser(String xml, XmlParseCallback callback)
				throws XmlPullParserException {
			// TODO Auto-generated constructor stub
			mCallback = callback;
			createParser(xml);
		}

		protected void start() throws XmlPullParserException, IOException {
			setXmlParseCallback(mCallback);
			startParse();
		}
	}

}
